package com.pccw.client.hessianclient;

import java.io.ObjectOutput;
import java.net.MalformedURLException;
import java.util.Map;

import com.caucho.hessian.client.HessianConnectionFactory;
import com.caucho.hessian.client.HessianProxyFactory;
import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.micro.MicroHessianInput;
import com.pccw.service.exception.InteractionException;
import com.pccw.service.iace.HelloHessian;
import com.pccw.service.iace.IEye;
import com.pccw.service.iace.IWeibo;
import com.pccw.service.iace.MyCar;

/**
 * @author michael
 *
 */
public class HessianClientTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
    	//executeWithHessianProxyFactory();
    	executeWeibo();
    //	executeWithServlet();
    	//executeHello();
    }
    
    public static void executeWeibo(){
        String url = "http://localhost:8080/HessianServer/remoting/weibo1";
        HessianProxyFactory proxyFactory = new HessianProxyFactory();
        HessianConnectionFactory connFactory=new MyConnectionFactory();
        proxyFactory.setConnectionFactory(connFactory);
        connFactory.setHessianProxyFactory(proxyFactory);

			try {
				IWeibo weibo;
				weibo = (IWeibo) proxyFactory.create(IWeibo.class, url);
				System.out.println(weibo.getContacts());
			} catch (Exception e) {
				System.out.println("Exception:"+ ((InteractionException)e).getTypeId());
				e.printStackTrace();
			}
			
			try {
				IWeibo weibo;
				weibo = (IWeibo) proxyFactory.create(IWeibo.class, url);
				System.out.println(weibo.getContacts());
			} catch (Exception e) {
				System.out.println("Exception:"+ ((InteractionException)e).getTypeId());
				e.printStackTrace();
			}
    }
    
    
    public static void executeWithServlet(){
        String url = "http://localhost:8080/HessianServer/hessianService";
        HessianProxyFactory factory = new HessianProxyFactory();
        factory.setUser("guxuede");
        factory.setPassword("dg23hhjs");
        try {
        		IEye hello = (IEye) factory.create(IEye.class, url);
        		hello.look();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    public static void executeHello(){
        String url = "http://localhost:8080/HessianServer/remoting/hello";
        HessianProxyFactory factory = new HessianProxyFactory();
        factory.setUser("guxuede");
        factory.setPassword("dg23hhjs");
        System.out.println(factory.getBasicAuth());
        try {
            HelloHessian hello = (HelloHessian) factory.create(HelloHessian.class, url);
            System.out.println(hello.sayHello());
            MyCar car = hello.getMyCar();
            System.out.println(car.toString());
            for (Map.Entry<String, String> entry : hello.myBabays().entrySet()) {
                System.out.println(entry.getKey() + "   " + entry.getValue());
            }

            for (String str : hello.myLoveFruit()) {
                System.out.println(str);
            }

            hello.save("This is a let");
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
    

}
